﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using GpCommon.Helpers;
using System.Windows.Automation;
using System.Windows;
using GpCommon.Logger;
using System.Diagnostics;
using System.Runtime.InteropServices;
using static GpCommon.Helpers.Win32Helper;

namespace FirstLimitUpMode.Handles
{
    public class AutoHelper
    {
        private const int EM_REPLACESEL = 0x00C2;
        private const int BM_SETCHECK = 0x00F1;
        private const int BST_CHECKED = 0x0001;
        //public static bool BuyExecute(string code, double trigPrice, double buyPrice, int count)
        //{
        //    try
        //    {
        //        IntPtr mainIntptr = Win32Helper.FindWindow(null, "网上股票交易系统5.0");
        //        if (mainIntptr != IntPtr.Zero)
        //        {
        //            AutomationElement rootAE = AutomationElement.FromHandle(mainIntptr);

        //            UIAutomationHelper.InvokeExpandCollapseAE(rootAE, AutomationElement.NameProperty, "智能交易");
        //            UIAutomationHelper.InvokeExpandCollapseAE(rootAE, AutomationElement.NameProperty, "条件下单");
        //            UIAutomationHelper.InvokeSelectionItemAE(rootAE, AutomationElement.NameProperty, "定价买入");
        //            IntPtr parentHwnd = IntPtr.Zero;
        //            List<IntPtr> editList = new List<IntPtr>();

        //            var contols = FindChildControls(mainIntptr, null);
        //            contols = FindChildControls(contols[0], "AfxMDIFrame140s");
        //            contols = FindChildControls(contols[0], null);
        //            for (int i = 0; i < contols.Count; i++)
        //            {
        //                var subcontols = FindChildControls(contols[i], "Edit");
        //                if (subcontols.Count == 4)
        //                {
        //                    parentHwnd = contols[i];
        //                    editList = subcontols;
        //                    break;
        //                }
        //            }
        //            if (editList.Count != 4 || parentHwnd == IntPtr.Zero)
        //                return false;
        //            // 代号
        //            Win32Helper.SendMessage(editList[0], EM_REPLACESEL, IntPtr.Zero, "");
        //            Win32Helper.SendMessage(editList[0], EM_REPLACESEL, IntPtr.Zero, code);

        //            //触发价格
        //            Win32Helper.SendMessage(editList[1], EM_REPLACESEL, IntPtr.Zero, trigPrice.ToString("F2"));

        //            // 自定义定价
        //            UIAutomationHelper.InvokeInvokeAE(rootAE, AutomationElement.AutomationIdProperty, "3610"); // 自定义
        //            //var intPtrTemp = FindChildControls(parentHwnd, "Button", "自定义");
        //            //SendMessage(intPtrTemp[0], BM_SETCHECK, BST_CHECKED, 0);

        //            // 委托价
        //            Win32Helper.SendMessage(editList[2], EM_REPLACESEL, IntPtr.Zero, buyPrice.ToString("F2"));

        //            //数量
        //            Win32Helper.SendMessage(editList[3], EM_REPLACESEL, IntPtr.Zero, count.ToString());

        //            UIAutomationHelper.InvokeInvokeAE(rootAE, AutomationElement.AutomationIdProperty, "3551"); // 勾选协议
        //            //intPtrTemp = FindChildControls(parentHwnd, "Button", "已阅读并同意");
        //            //SendMessage(intPtrTemp[0], BM_SETCHECK, BST_CHECKED, 0);


        //            //Stopwatch watch = new Stopwatch();
        //            //watch.Start();
        //            //while (watch.ElapsedMilliseconds < 2000)
        //            //{
        //            //    Thread.Sleep(100);
        //            //    if (UIAutomationHelper.InvokeInvokeAE(rootAE, AutomationElement.AutomationIdProperty, "1006")) // 创建
        //            //        break;
        //            //}




        //            //watch.Restart();
        //            //while (watch.ElapsedMilliseconds < 2000)
        //            //{
        //            //    Thread.Sleep(100);
        //            //    if (UIAutomationHelper.InvokeInvokeAE(rootAE, AutomationElement.AutomationIdProperty, "6")) // 创建
        //            //        break;
        //            //}
        //        }
        //    }
        //    catch (Exception ex)
        //    {
        //        Logger.Error(ex);
        //        return false;
        //    }
        //    return true;
        //}

        //public static bool SaleExecute(string code, double trigPrice, double salePrice, int count, bool isAll)
        //{
        //    try
        //    {
        //        IntPtr mainIntptr = Win32Helper.FindWindow(null, "网上股票交易系统5.0");
        //        if (mainIntptr != IntPtr.Zero)
        //        {
        //            AutomationElement rootAE = AutomationElement.FromHandle(mainIntptr);

        //            UIAutomationHelper.InvokeExpandCollapseAE(rootAE, AutomationElement.NameProperty, "智能交易");
        //            UIAutomationHelper.InvokeExpandCollapseAE(rootAE, AutomationElement.NameProperty, "条件下单");
        //            UIAutomationHelper.InvokeSelectionItemAE(rootAE, AutomationElement.NameProperty, "定价卖出");
        //            IntPtr parentHwnd = IntPtr.Zero;
        //            List<IntPtr> editList = new List<IntPtr>();

        //            var contols = FindChildControls(mainIntptr, null);
        //            contols = FindChildControls(contols[0], "AfxMDIFrame140s");
        //            contols = FindChildControls(contols[0], null);
        //            if (contols.Count != 7)
        //                return false;
        //            var subcontols = FindChildControls(contols[6], "Edit");
        //            if (subcontols.Count == 4)
        //            {
        //                parentHwnd = contols[6];
        //                editList = subcontols;
        //            }
        //            if (editList.Count != 4 || parentHwnd == IntPtr.Zero)
        //                return false;
        //            // 代号
        //            Win32Helper.SendMessage(editList[0], EM_REPLACESEL, IntPtr.Zero, "");
        //            Win32Helper.SendMessage(editList[0], EM_REPLACESEL, IntPtr.Zero, code);

        //            //触发价格
        //            Win32Helper.SendMessage(editList[1], EM_REPLACESEL, IntPtr.Zero, trigPrice.ToString("F2"));

        //            // 自定义定价
        //            //UIAutomationHelper.InvokeInvokeAE(rootAE, AutomationElement.AutomationIdProperty, "3610"); // 自定义
        //            var intPtrTemp = FindChildControls(parentHwnd, "Button", "自定义");
        //            Win32Helper.SendMessage(intPtrTemp[0], BM_SETCHECK, BST_CHECKED, 0);

        //            // 委托价
        //            Win32Helper.SendMessage(editList[2], EM_REPLACESEL, IntPtr.Zero, salePrice.ToString("F2"));

        //            if (isAll)
        //            {
        //                Stopwatch watch = new Stopwatch();
        //                watch.Start();
        //                int num = 0;
        //                while (watch.ElapsedMilliseconds < 1000)
        //                {
        //                    Thread.Sleep(100);
        //                    num = UIAutomationHelper.GetIntValue(rootAE, AutomationElement.AutomationIdProperty, "1018"); // 创建
        //                    if (num > 0)
        //                        break;
        //                }
        //                if(num > 0)
        //                {
        //                    //数量
        //                    Win32Helper.SendMessage(editList[3], EM_REPLACESEL, IntPtr.Zero, num.ToString());
        //                }
        //                ////全仓
        //                //UIAutomationHelper.InvokeInvokeAE(rootAE, AutomationElement.AutomationIdProperty, "1176"); // 全仓
        //            }
        //            else
        //            {
        //                //数量
        //                Win32Helper.SendMessage(editList[3], EM_REPLACESEL, IntPtr.Zero, count.ToString());
        //            }


        //            UIAutomationHelper.InvokeInvokeAE(rootAE, AutomationElement.AutomationIdProperty, "3551"); // 勾选协议
        //            //intPtrTemp = FindChildControls(parentHwnd, "Button", "已阅读并同意");
        //            //SendMessage(intPtrTemp[0], BM_SETCHECK, BST_CHECKED, 0);

        //            //Stopwatch watch = new Stopwatch();
        //            //watch.Start();
        //            //while (watch.ElapsedMilliseconds < 2000)
        //            //{
        //            //    Thread.Sleep(100);
        //            //    if (UIAutomationHelper.InvokeInvokeAE(rootAE, AutomationElement.AutomationIdProperty, "1006")) // 创建
        //            //        break;
        //            //}
        //            //watch.Restart();
        //            //while (watch.ElapsedMilliseconds < 2000)
        //            //{
        //            //    Thread.Sleep(100);
        //            //    if (UIAutomationHelper.InvokeInvokeAE(rootAE, AutomationElement.AutomationIdProperty, "6")) // 创建
        //            //        break;
        //            //}
        //        }
        //    }
        //    catch (Exception ex)
        //    {
        //        Logger.Error(ex);
        //        return false;
        //    }
        //    return true;
        //}

        public static bool BuyExecute(string code, double trigPrice, double buyPrice, int count)
        {
            try
            {
                IntPtr mainIntptr = Win32Helper.FindWindow(null, "网上股票交易系统5.0");
                if (mainIntptr != IntPtr.Zero)
                {
                    Win32Helper.ShowWindow(mainIntptr, SW_SHOW);
                    AutomationElement rootAE = AutomationElement.FromHandle(mainIntptr);

                    UIAutomationHelper.InvokeExpandCollapseAE(rootAE, AutomationElement.NameProperty, "智能交易");
                    UIAutomationHelper.InvokeExpandCollapseAE(rootAE, AutomationElement.NameProperty, "条件下单");
                    UIAutomationHelper.InvokeSelectionItemAE(rootAE, AutomationElement.NameProperty, "定价买入");

                    // 代号
                    var codePt = UIAutomationHelper.GetMidPosition(rootAE, AutomationElement.AutomationIdProperty, "1032");
                    MouseFlag.MouseLeftDoubleClickEvent((int)codePt.X, (int)codePt.Y, 0);

                    foreach (var item in code)
                    {
                        KeyBoard.keyPress(KeyPressByteConvert(item));
                    }
                    //触发价格
                    var executePt = UIAutomationHelper.GetMidPosition(rootAE, AutomationElement.AutomationIdProperty, "1070");

                    MouseFlag.MouseLeftDoubleClickEvent((int)executePt.X, (int)executePt.Y, 0);
                    var str97 = trigPrice.ToString("F2");
                    foreach (var item in str97)
                    {
                        KeyBoard.keyPress(KeyPressByteConvert(item));
                    }

                    // 自定义定价
                    UIAutomationHelper.InvokeInvokeAE(rootAE, AutomationElement.AutomationIdProperty, "3610"); // 自定义

                    // 委托价
                    var buyInPt = UIAutomationHelper.GetMidPosition(rootAE, AutomationElement.AutomationIdProperty, "1068");
                    MouseFlag.MouseLeftDoubleClickEvent((int)buyInPt.X, (int)buyInPt.Y, 0);
                    var strBuy = buyPrice.ToString("F2");
                    foreach (var item in strBuy)
                    {
                        KeyBoard.keyPress(KeyPressByteConvert(item));
                    }

                    //数量
                    var numPt = UIAutomationHelper.GetMidPosition(rootAE, AutomationElement.AutomationIdProperty, "1069");
                    MouseFlag.MouseLeftDoubleClickEvent((int)numPt.X, (int)numPt.Y, 0);
                    foreach (var item in count.ToString())
                    {
                        KeyBoard.keyPress(KeyPressByteConvert(item));
                    }

                    UIAutomationHelper.InvokeInvokeAE(rootAE, AutomationElement.AutomationIdProperty, "3551"); // 勾选协议

                    UIAutomationHelper.InvokeInvokeAE(rootAE, AutomationElement.AutomationIdProperty, "1006"); // 创建
                    //intPtrTemp = FindChildControls(parentHwnd, "Button", "已阅读并同意");
                    //SendMessage(intPtrTemp[0], BM_SETCHECK, BST_CHECKED, 0);


                    //Stopwatch watch = new Stopwatch();
                    //watch.Start();
                    //while (watch.ElapsedMilliseconds < 2000)
                    //{
                    //    Thread.Sleep(100);
                    //    if (UIAutomationHelper.InvokeInvokeAE(rootAE, AutomationElement.AutomationIdProperty, "1006")) // 创建
                    //        break;
                    //}




                    //watch.Restart();
                    //while (watch.ElapsedMilliseconds < 2000)
                    //{
                    //    Thread.Sleep(100);
                    //    if (UIAutomationHelper.InvokeInvokeAE(rootAE, AutomationElement.AutomationIdProperty, "6")) // 创建
                    //        break;
                    //}
                }
            }
            catch (Exception ex)
            {
                Logger.Error(ex);
                return false;
            }
            return true;
        }

        public static bool SaleExecute(string code, double trigPrice, double salePrice, int count, bool isAll)
        {
            try
            {
                IntPtr mainIntptr = Win32Helper.FindWindow(null, "网上股票交易系统5.0");
                if (mainIntptr != IntPtr.Zero)
                {
                    Win32Helper.ShowWindow(mainIntptr, SW_SHOW);
                    AutomationElement rootAE = AutomationElement.FromHandle(mainIntptr);

                    UIAutomationHelper.InvokeExpandCollapseAE(rootAE, AutomationElement.NameProperty, "智能交易");
                    UIAutomationHelper.InvokeExpandCollapseAE(rootAE, AutomationElement.NameProperty, "条件下单");
                    UIAutomationHelper.InvokeSelectionItemAE(rootAE, AutomationElement.NameProperty, "定价卖出");

                    // 代号
                    var codePt = UIAutomationHelper.GetMidPosition(rootAE, AutomationElement.AutomationIdProperty, "1032");
                    MouseFlag.MouseLeftDoubleClickEvent((int)codePt.X, (int)codePt.Y, 0);

                    foreach (var item in code)
                    {
                        KeyBoard.keyPress(KeyPressByteConvert(item));
                    }
                    //触发价格
                    var executePt = UIAutomationHelper.GetMidPosition(rootAE, AutomationElement.AutomationIdProperty, "1070");

                    MouseFlag.MouseLeftDoubleClickEvent((int)executePt.X, (int)executePt.Y, 0);
                    var str97 = trigPrice.ToString("F2");
                    foreach (var item in str97)
                    {
                        KeyBoard.keyPress(KeyPressByteConvert(item));
                    }

                    // 自定义定价
                    UIAutomationHelper.InvokeInvokeAE(rootAE, AutomationElement.AutomationIdProperty, "3610"); // 自定义

                    // 委托价
                    var buyInPt = UIAutomationHelper.GetMidPosition(rootAE, AutomationElement.AutomationIdProperty, "1068");
                    MouseFlag.MouseLeftDoubleClickEvent((int)buyInPt.X, (int)buyInPt.Y, 0);
                    var strPrice = salePrice.ToString("F2");
                    foreach (var item in strPrice)
                    {
                        KeyBoard.keyPress(KeyPressByteConvert(item));
                    }



                    if (isAll)
                    {
                        Stopwatch watch = new Stopwatch();
                        watch.Start();
                        int num = 0;
                        while (watch.ElapsedMilliseconds < 1000)
                        {
                            Thread.Sleep(100);
                            num = UIAutomationHelper.GetIntValue(rootAE, AutomationElement.AutomationIdProperty, "1018"); // 创建
                            if (num > 0)
                                break;
                        }
                        if (num > 0)
                        {
                            //数量
                            var numPt = UIAutomationHelper.GetMidPosition(rootAE, AutomationElement.AutomationIdProperty, "1069");
                            MouseFlag.MouseLeftDoubleClickEvent((int)numPt.X, (int)numPt.Y, 0);
                            foreach (var item in num.ToString())
                            {
                                KeyBoard.keyPress(KeyPressByteConvert(item));
                            }
                        }
                        ////全仓
                        //UIAutomationHelper.InvokeInvokeAE(rootAE, AutomationElement.AutomationIdProperty, "1176"); // 全仓
                    }
                    else
                    {
                        //数量
                        var numPt = UIAutomationHelper.GetMidPosition(rootAE, AutomationElement.AutomationIdProperty, "1069");
                        MouseFlag.MouseLeftDoubleClickEvent((int)numPt.X, (int)numPt.Y, 0);
                        foreach (var item in count.ToString())
                        {
                            KeyBoard.keyPress(KeyPressByteConvert(item));
                        }
                    }


                    UIAutomationHelper.InvokeInvokeAE(rootAE, AutomationElement.AutomationIdProperty, "3551"); // 勾选协议

                    UIAutomationHelper.InvokeInvokeAE(rootAE, AutomationElement.AutomationIdProperty, "1006"); // 创建

                    //intPtrTemp = FindChildControls(parentHwnd, "Button", "已阅读并同意");
                    //SendMessage(intPtrTemp[0], BM_SETCHECK, BST_CHECKED, 0);

                    //Stopwatch watch = new Stopwatch();
                    //watch.Start();
                    //while (watch.ElapsedMilliseconds < 2000)
                    //{
                    //    Thread.Sleep(100);
                    //    if (UIAutomationHelper.InvokeInvokeAE(rootAE, AutomationElement.AutomationIdProperty, "1006")) // 创建
                    //        break;
                    //}
                    //watch.Restart();
                    //while (watch.ElapsedMilliseconds < 2000)
                    //{
                    //    Thread.Sleep(100);
                    //    if (UIAutomationHelper.InvokeInvokeAE(rootAE, AutomationElement.AutomationIdProperty, "6")) // 创建
                    //        break;
                    //}
                }
            }
            catch (Exception ex)
            {
                Logger.Error(ex);
                return false;
            }
            return true;
        }
        private static List<IntPtr> FindChildControls(IntPtr hWndParent, string className, string windowName = null)
        {
            List<IntPtr> handles = new List<IntPtr>();
            IntPtr hWndChildAfter = IntPtr.Zero;

            while (true)
            {
                IntPtr childHandle = Win32Helper.FindWindowEx(hWndParent, hWndChildAfter, className, windowName);
                if (childHandle == IntPtr.Zero)
                    break;
                handles.Add(childHandle);
                hWndChildAfter = childHandle;
            }
            return handles;
        }

        private static byte KeyPressByteConvert(char c)
        {
            switch (c)
            {
                case '0':
                    return KeyBoard.vKey0;
                case '1':
                    return KeyBoard.vKey1;
                case '2':
                    return KeyBoard.vKey2;
                case '3':
                    return KeyBoard.vKey3;
                case '4':
                    return KeyBoard.vKey4;
                case '5':
                    return KeyBoard.vKey5;
                case '6':
                    return KeyBoard.vKey6;
                case '7':
                    return KeyBoard.vKey7;
                case '8':
                    return KeyBoard.vKey8;
                case '9':
                    return KeyBoard.vKey9;
                case '.':
                    return KeyBoard.vKeyDecimal;
            }
            return KeyBoard.vKey0;
        }

    }
}
